package alioss

import(
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"sync"
	"gitee.com/Cookie_XiaoD/go-filestore-server/config"
	"log"
	"fmt"
)

var ossCli *oss.Client


var once sync.Once

//GetClient 获取阿里云OSS Client
func GetClient() *oss.Client{
	once.Do(func() {
		var err error
		ossCli,err=oss.New(config.OSSCfg.EndPoint,config.OSSCfg.AccessKeyID,config.OSSCfg.AccessKeySecret)
		if err!=nil{
			log.Printf("创建阿里云oss的client失败：%v",err.Error())
		}
	})
	return ossCli
}


//GetBucket 获取oss bucket
func GetBucket() *oss.Bucket{
	client:=GetClient()
	if client==nil{
		return nil
	}
	bucket,err:=client.Bucket(config.OSSCfg.Bucket)
	if err!=nil{
		log.Printf("获取Bucket失败：%v",err.Error())
		return nil
	}
	return bucket
}

// GetSignedURL : 获取临时签名的下载url
func GetSignedURL(key string) string {
	//600秒过期
	signedURL, err := GetBucket().SignURL(key, oss.HTTPGet, 600)
	if err != nil {
		fmt.Println(err.Error())
		return ""
	}
	return signedURL
}
